package com.translation.dao.sql;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class QueryCache {

	private Map<String, String> queries;

	public QueryCache() {
		queries = new ConcurrentHashMap<String, String>();
	}

	public String getQuery(String name) throws IOException {
		String query = null;
		query = queries.get(name);
		if (query == null) {
			query = getSQL(name);
			queries.put(name, query);
		}
		return query;
	}

	private String getSQL(String script) throws IOException {
		Reader r = null;
		try {
			r = new InputStreamReader(QueryCache.class.getResourceAsStream(script), "UTF-8");
			StringBuilder sb = new StringBuilder();
			int c = 0;
			while ((c = r.read()) != -1) {
				sb.append((char) c);
			}
			return sb.toString();
		} finally {
			if (r != null) {
				r.close();
			}
		}
	}

	public String getQuery(String name, String replace) throws IOException {
		String query = null;
		boolean empty = replace == null || "".equals(replace);
		String key = (empty) ? name : name + replace;
		query = queries.get(key);
		if (query == null) {
			query = getSQL(name);
			if (!empty) {
				query = query.replace(replace, "");
			}
			queries.put(key, query);
		}
		return query;
	}
}
